﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class Shop_OnlineSurvey_Manager : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    override protected void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        RequestLoginPage = "Shop/OnlineSurvey_Manager.aspx";

        base.OnInit(e);
    }

    #endregion

    private void BindData()
    {
        DataTable dt = new DAL.Tables.tbl_Site_OnlineSurvey().Open("", "", "ID");

        if (dt == null)
        {
            return;
        }

        g.DataSource = dt;
        g.DataBind();        
    }

    protected void g_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            DataRow dr = drv.Row;

            bool IsShow = Shove._Convert.StrToBool(dr["IsShow"].ToString(), false);
            Button btnStart = (Button)e.Item.Cells[5].FindControl("btnStart");
            Button btnStop = (Button)e.Item.Cells[5].FindControl("btnStop");

            if (IsShow)
            {
                e.Item.Cells[4].Text = "启用";
                btnStart.Enabled = false; 
            }
            else
            {
                e.Item.Cells[4].Text = "停用";
                btnStop.Enabled = false;
            }

        }
    }

    protected void g_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        {
            long ID = Shove._Convert.StrToLong(e.Item.Cells[0].Text, -1);
            DAL.Tables.tbl_Site_OnlineSurvey table = new DAL.Tables.tbl_Site_OnlineSurvey();

            if (e.CommandName == "Start")
            {
                table.DateTime.Value = DateTime.Now;
                table.IsShow.Value = 1;

                if (table.Update("[ID]=" + ID) >= 0)
                {
                    Shove._Web.JavaScript.Alert(this.Page, "启用成功");
                }
            }

            else if (e.CommandName == "Stop")
            {
                table.DateTime.Value = DateTime.Now;
                table.IsShow.Value = 0;

                if (table.Update("[ID]=" + ID) >= 0)
                {
                    Shove._Web.JavaScript.Alert(this.Page, "停用成功");
                }
            }

            BindData();
        }
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("OnlineSurvey_Edit.aspx");
    }

}
